<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Administrator_song extends MY_Administrator {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/home
	 *	- or -  
	 * 		http://example.com/index.php/home/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
    function __construct()
    {
        parent::__construct();		
		//Load Model
		$this->load->model('administrator_song_model');
    }
	
	/*Song Management*/
	public function content_management($module=NULL,$arg=NULL)
	{					
		//Load ColorBox
		$this->data['js'][] = "jquery.colorbox-min.js";	
		$this->data['css'][] = "colorbox.css";
					
		switch($module):
			case 'add':		
				//POST
				if(isset($_POST) && !empty($_POST)):
					$this->administrator_song_model->content_management_add();
					redirect($this->session->userdata('returnUrl'));
				endif;

				//Load Ckeditor
				$this->load->helper('ckeditor');
				//Load Datetime
				$this->data['js'][] = "jquery-ui-1.9.1.custom.min.js";	
				$this->data['js'][] = "jquery-ui-timepicker-addon.js";	
				$this->data['js'][] = "jquery-ui-sliderAccess.js";	
				$this->data['css'][] = "jquery-ui-1.9.1.custom.min.css";				
				//Load Uploadify
				$this->data['js'][] = "uploadify/jquery.uploadify.v2.1.4.min.js";
				$this->data['css'][] = "uploadify/uploadify.css";									
				
				$this->data['dataMediaType'] = $this->db->order_by('media_type_name','ASC')->get('media_type')->result();
				$this->data['dataGenre'] = $this->db->order_by('genre_name','ASC')->where_not_in('genre_id',array(15,16,17,30))->get('genre')->result();
				$this->data['dataLabel'] = $this->db->order_by('label_name','ASC')->get('label')->result();				
				
				
				$this->data['content'] = "administrator/song/content_management_add";
				$this->load->view("templates/".$this->folder_templates."templates", $this->data);
			break;
			case 'edit':
				$id = intval($arg);	
				//Check Id exist
				$is_exist = $this->db->from('song')
				->where(array('song_id' => $id))
				->count_all_results();
				if(empty($is_exist)) show_error('This id is not information.');	
									
				//POST
				if(isset($_POST) && !empty($_POST)):
					$this->administrator_song_model->content_management_edit($id);
					redirect($this->session->userdata('returnUrl'));			
				endif;
				
				//Load Ckeditor
				$this->load->helper('ckeditor');
				//Load Datetime
				$this->data['js'][] = "jquery-ui-1.9.1.custom.min.js";	
				$this->data['js'][] = "jquery-ui-timepicker-addon.js";	
				$this->data['js'][] = "jquery-ui-sliderAccess.js";	
				$this->data['css'][] = "jquery-ui-1.9.1.custom.min.css";				
				//Load Uploadify
				$this->data['js'][] = "uploadify/jquery.uploadify.v2.1.4.min.js";
				$this->data['css'][] = "uploadify/uploadify.css";								

				$this->data['dataMediaType'] = $this->db->order_by('media_type_name','ASC')->get('media_type')->result();
				$this->data['dataGenre'] = $this->db->order_by('genre_name','ASC')->where_not_in('genre_id',array(15,16,17,30))->get('genre')->result();
				$this->data['dataLabel'] = $this->db->order_by('label_name','ASC')->get('label')->result();
				
				$this->data['dataSongArtist'] = $this->db->select('j.artist_id,j.artist_name,t.is_default')
				->join('artist_info as j','j.artist_id=t.artist_id','INNER')
				->get_where('song_artist as t',array('t.song_id' => $id))
				->result();
				$this->data['dataSongAlbum'] = $this->db->select('j.album_id,j.album_name,t.is_default')
				->join('album_info as j','j.album_id=t.album_id','INNER')
				->get_where('song_album as t',array('t.song_id' => $id))
				->result();	
				$this->data['dataSongVideo'] = $this->db->select('j.video_id,j.video_name')
				->join('video_info as j','j.video_id=t.video_id','INNER')
				->get_where('video_song as t',array('t.song_id' => $id))
				->row();
				$dataSongGenre = $this->db->select('genre_id')->get_where('song_genre',array('song_id' => $id))->result();
				$this->data['dataSongGenre'] = array();
				foreach($dataSongGenre as $value):
					$this->data['dataSongGenre'][] = $value->genre_id;
				endforeach;
				$this->data['dataChordsRelate'] = $this->db->select('t.chords_id,j.song_name')
				->join('chords_info as j','j.chords_id=t.chords_id','INNER')
				->get_where('chords as t',array('t.song_id' => $id),1)
				->row();
				
				$this->data['dataResult'] = $this->db
				->join('song_info as j','j.song_id=t.song_id','INNER')
				->get_where('song as t',array('t.song_id' => $id),1)
				->row();
	
				$this->data['content'] = "administrator/song/content_management_edit";
				$this->load->view("templates/".$this->folder_templates."templates", $this->data);		
			break;			
			case 'delete':
				$this->administrator_song_model->content_management_delete();	
				redirect($this->session->userdata('returnUrl'));		
			break;												
			default:
				/*Keep Url*/
				$this->session->set_userdata('returnUrl', base_url($this->input->server('REQUEST_URI')));
					
				/*Order Condition*/
				$orderby = $this->input->get('orderby');
				$sort = $this->input->get('sort');
				$orderCondition = "song_id DESC";
				if(!empty($orderby) && !empty($sort)):
					switch($orderby):
						case 'create_date':
						case 'update_date':
							$orderCondition = "t.".$orderby." ".$sort;
						break;
						default:
							$orderCondition = $orderby." ".$sort;
					endswitch;
				endif;
					
				/*Search Condition*/
				$searchCondition = array('t.song_id IS NOT NULL');
				$song_id = $this->input->get('song_id');
				$media_type_id = $this->input->get('media_type_id');				
				$genre_id = $this->input->get('genre_id');				
				$song_name = addslashes($this->input->get('song_name'));
				$song_description = addslashes($this->input->get('song_description'));
				$update_by = addslashes($this->input->get('update_by'));
				if(!empty($song_id)) $searchCondition[] = "t.song_id LIKE '%".$song_id."%'";
				if(!empty($media_type_id)) $searchCondition[] = "t.media_type_id = '".$media_type_id."'";				
				if(!empty($genre_id)) $searchCondition[] = "t.genre_id = '".$genre_id."'";				
				if(!empty($song_name)) $searchCondition[] = "j.song_name LIKE '%".$song_name."%'";
				if(!empty($song_description)) $searchCondition[] = "j.song_description LIKE '%".$song_description."%'";
				if(!empty($update_by)) $searchCondition[] = "t.update_by LIKE '%".$update_by."%'";
				
			  	/*Pager*/		
				$config['base_url'] = current_url().'?'.query_string(array('per_page'));
				$config['total_rows'] = $this->db->from('song as t')
				->join('song_info as j','j.song_id=t.song_id','INNER')
				->where(''.implode(' AND ',$searchCondition).'')
				->count_all_results();
				$config['per_page'] = 20;
				$config['num_links'] = 10;
				$config['page_query_string'] = TRUE;
			  	$this->pagination->initialize($config);
				$this->data['pager'] = $this->pagination->create_links();
				$this->data['dataResult'] = $this->db->select('t.*,j.song_name,j.song_description')->join('song_info as j','j.song_id=t.song_id','LEFT')->order_by($orderCondition)->get_where('song as t',''.implode(' AND ',$searchCondition).'',$config['per_page'],intval($this->input->get('per_page')))->result();
				
				$this->data['dataMediaType'] = $this->db->order_by('media_type_name','ASC')->get('media_type')->result();
				$this->data['dataGenreType'] = $this->db->order_by('genre_name','ASC')->get('genre')->result();
				
				
				$this->data['content'] = "administrator/song/content_management";
				$this->load->view("templates/".$this->folder_templates."templates", $this->data);
		endswitch;		
	}
	
	public function statics($module=NULL,$arg=NULL)
	{
		//Load Calendar
		$this->data['js'][] = "jquery.calendar.js";
		$this->data['css'][] = "calendar.css";
		
		/*Search Condition*/
		$searchCondition = array();
		$media_type_id = $this->input->get('media_type_id');
		$start_date = $this->input->get('start_date');
		$end_date = $this->input->get('end_date');
		if($start_date && $end_date):
			$start_date_arr = explode('/',$start_date);
			$end_date_arr = explode('/',$end_date);
			$searchCondition[] = "t.update_date >= '".$start_date_arr[2]."-".$start_date_arr[1]."-".$start_date_arr[0]."' AND t.update_date <= '".$end_date_arr[2]."-".$end_date_arr[1]."-".$end_date_arr[0]." 23:59:59'";
		else:
			$searchCondition[] = "t.update_date >= '".date('Y-m-d')."'";
		endif;
		if(!empty($media_type_id)) $searchCondition[] = "k.media_type_id = ".$media_type_id."";
		
		$this->data['dataResult'] = $this->db->select('t.song_id,j.song_name,SUM(t.song_click_views) as views,k.create_date,t.update_date')->
		join('song_info as j',"j.song_id=t.song_id",'INNER')->
		join('song as k',"k.song_id=j.song_id",'INNER')->
		order_by('views DESC')->
		group_by('t.song_id')->
		get_where('song_click_stat as t',''.implode(' AND ',$searchCondition).'',100)->
		result();
		foreach($this->data['dataResult'] as $value):
			$value->artist = $this->db->select('j.artist_name')
			->join('artist_info as j','j.artist_id=t.artist_id','INNER')
			->get_where('song_artist as t',array('t.song_id' => $value->song_id,'is_default' => '1'))
			->result();
		endforeach;	
		
		$this->data['dataMediaType'] = $this->db->order_by('media_type_name','ASC')->get('media_type')->result();
		
/*		$DB_EE = $this->load->database("you2play_ee",TRUE);
		
		$SQL = "SELECT * FROM `exp_chart` WHERE 1 ".$date_interval." GROUP BY entry_id ORDER BY views DESC LIMIT 20";
		$this->data['dataResult'] = $DB_EE->query($SQL)->result();
		//Close Database
		$DB_EE->close();
		$this->db->close();
		$this->load->database("you2play_2013",TRUE);*/
						
		$this->data['content'] = "administrator/song/statics";
		$this->load->view("templates/".$this->folder_templates."templates", $this->data);		
	}
}